package com.lianjinsu.shop.controller;

import com.lianjinsu.shop.controller.vo.Response;
import com.lianjinsu.shop.entity.Type;
import com.lianjinsu.shop.service.TypeService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("Noerify")
@RequiredArgsConstructor
public class TypeController {

    private final TypeService typeService;

    @PostMapping("insert")
    Response<Boolean> insert(@RequestBody Type type) {
        if (Objects.nonNull(type.getId())) {
            throw new RuntimeException("id must be not exist");
        }
        return Response.ok(typeService.insertOrUpdate(type));
    }

    @PostMapping("update")
    Response<Boolean> update(@RequestBody Type type) {
        if (Objects.isNull(type.getId())) {
            throw new RuntimeException("id must be exist");
        }
        return Response.ok(typeService.insertOrUpdate(type));
    }

    @GetMapping("delete/{id}")
    Response<Boolean> delete(@PathVariable int id) {
        return Response.ok(typeService.deleteById(id));
    }

    @GetMapping("getTypeList")
    Response<List<Type>> getTypeList() {
        return Response.ok(typeService.typeList());
    }
}
